#include "widget.h"
#include "ui_widget.h"
#include "topo.h"
#include "check.h"
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    Widget::on_pushButton_clicked();
    QApplication::setStyle("Fusion");

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_add_class_clicked()
{
    a=new add_data;
    a->show();
    connect(a,&add_data::newDataCome,this,&Widget::on_pushButton_clicked);//即时保存
}


void Widget::on_check_result_clicked()
{
    topo *t=new topo();
    t->toposort();
    delete t;
    check *c=new check;
    c->show();
    c->show_time();
}


void Widget::on_pushButton_clicked()
{

    table = ui->tableView;
    model = new QStandardItemModel(table);
    model->setHorizontalHeaderLabels(QStringList() << "课程名称" << "学时" << "课程编码" << "先修课程号"); // 设置表头
    table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//拉伸单元格铺满屏幕

    QString workingDirectory = QDir::toNativeSeparators(QDir::current().filePath("data.txt"));


    QFile file(workingDirectory); //路径选择
    if (file.open(QIODevice::ReadOnly)) {
        QTextStream in(&file);
        while (!in.atEnd()) {
            QString lines = in.readLine();
            // 这里每行有4字段
            QStringList fields = lines.split(','); // 使用逗号作为分隔符
            QList<QStandardItem*> items;
            foreach (const QString& field, fields) {
                items.append(new QStandardItem(field));
            }
            model->appendRow(items);
        }
        file.close();
    } else {
        qDebug() << "Unable to open file"; // 如果无法打开文件，输出错误信息
    }
    table->setModel(model);
    table->show();
}


void Widget::on_del_class_clicked()
{
    table = ui->tableView;
    QItemSelectionModel *selectionModel = table->selectionModel();
    QModelIndexList selectedRows = selectionModel->selectedRows();

    if (!selectedRows.isEmpty()) {
        QMessageBox::StandardButton advise;
        advise = QMessageBox::question(this, "删除行", "您确定要删除选中的所有行吗？", QMessageBox::Yes|QMessageBox::No);
        if (advise == QMessageBox::Yes) {
            std::sort(selectedRows.begin(), selectedRows.end());  // 对选中的行号进行排序便于多个删除
            for (int i = selectedRows.count() - 1; i >= 0; --i) {
                table->model()->removeRow(selectedRows.at(i).row());//删除指令
            }
        }
    }

    Widget::save();
}

void Widget::on_kill_clicked()
{
    this->close();
}

void Widget::save()
{
    // 获取表格数据
    QAbstractItemModel *mod = ui->tableView->model();
    int rowCount = mod->rowCount();
    int columnCount = mod->columnCount();

    //找到文件的存储路径
    QString workingDirectory = QDir::toNativeSeparators(QDir::current().filePath("data.txt"));

    // 将表格数据写入文件
    QFile file(workingDirectory);
    if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        QTextStream stream(&file);
        for (int row = 0; row < rowCount; ++row) {
            for (int column = 0; column < columnCount; ++column) {
                QModelIndex index = mod->index(row, column);
                QString data = mod->data(index, Qt::DisplayRole).toString();//调用表格的数据并转化为QString
                stream << data;//写入
                if (column < columnCount - 1) {
                    stream << ","; // 添加逗号分隔符
                }
            }
            stream << "\n";  // 换行
        }
        file.close();
    } else {
        qDebug() << "Failed to open file for writing.";
    }
}
void Widget::on_save_clicked(){
    Widget::save();
    QMessageBox msgBox;
    msgBox.showNormal();
    msgBox.setWindowTitle("保存");
    msgBox.setText("保存成功！");
    msgBox.setStandardButtons(QMessageBox::Ok);
    msgBox.exec();
}
QStringList Widget::getTableViewData(int cul){
    QAbstractItemModel *mod = ui->tableView->model();
    int rowCount = mod->rowCount();
    QStringList rowData;
    for (int row = 0; row < rowCount; ++row) {
        QModelIndex index = mod->index(row,cul);
        QString data = mod->data(index, Qt::DisplayRole).toString();
        rowData.append(data);
    }
    return rowData;
}

int score_value=100;

int Widget::getSpinbox(){
    return score_value;
}

void Widget::on_score_valueChanged(int arg1)
{
    score_value=arg1;
}

